#import "layer_library_main.h"

@implementation layer_library_main

- (id)initLibraryMain
{
	CCSprite *_Background;
	CCSprite *_Box;
	CCMenuItemImage *_ItemExperiment;
	CCMenuItemImage *_ItemTreasuer;
	CCMenuItemImage *_ItemCredits;
	CCMenu *_Menu;

	if ((self = [super init]))
	{
		/*load resource
		*/
		reLoadResource(&g_CfgLibraryMainResourceTable);

		/*background
		*/
		_Background = [[CCSprite alloc] init];
		[self addChild:_Background];
		[_Background release];
		[_Background runAction:tcCreateRepeatAnimate(&g_CfgLibraryMainAnimationBackground)];
		_Background.scale = 4;
		_Background.positionInPixels = ccp(g_ScrrenSize.width / 2, g_ScrrenSize.height / 2);

		/*box
		*/
		_Box = uiCreateImageObject(&g_CfgLibraryMainImageBox, self);

		/*menu
		*/
		_ItemExperiment = uiCreateMenuItemImageObject(&g_CfgLibraryMainItemExperiment, self, @selector(CallBackItemExperiment:));
		_ItemTreasuer = uiCreateMenuItemImageObject(&g_CfgLibraryMainItemTreasure, self, @selector(CallBackItemTreasuer:));
		_ItemCredits = uiCreateMenuItemImageObject(&g_CfgLibraryMainItemCredits, self, @selector(CallBackItemCredits:));

		itemBack_ = CREATE_BTN_BACK(CallBackItemBack);
		itemBack_.anchorPoint = ccp(0.9f, 0.0f);
		itemBack_.positionInPixels = ccp(g_ScrrenSize.width, 0.0f);

		_Menu = uiNodeAddMenu(self, CGPointZero, _ItemExperiment, _ItemTreasuer, _ItemCredits, itemBack_, nil);

		/*flag
		*/
		flagFunctionLock_ = false;
	}
	return self;
}

- (void)dealloc
{
	/*release resource
	*/
	reFreeResource(&g_CfgLibraryMainResourceTable);

	[super dealloc];
}

- (void)CallBackItemExperiment:(id)Sender
{
	if (flagFunctionLock_)
	{
		return;
	}

	/*effect
	*/
	EffectButton();

	[g_SceneLibrary SetState:SCENE_LIBRARY_STATE_EXPERIMENT];
}

- (void)CallBackItemTreasuer:(id)Sender
{
	if (flagFunctionLock_)
	{
		return;
	}

	/*effect
	*/
	EffectButton();

	[g_SceneLibrary SetState:SCENE_LIBRARY_STATE_TREASURE];
}

- (void)CallBackItemCredits:(id)Sender
{
	if (flagFunctionLock_)
	{
		return;
	}

	/*effect
	*/
	EffectButton();

	[g_SceneLibrary SetState:SCENE_LIBRARY_STATE_CREDITS];
}

- (void)CallBackItemBack:(id)Sender
{
	CCSequence *_Seq;
	id _Action;
	CCCallFuncN *_CallBack;

	if (flagFunctionLock_)
	{
		return;
	}

	/*effect
	*/
	EffectButton();

	flagFunctionLock_ = true;
	_Action = [action_move_in_pixel actionWithDuration:BTN_MOVE_TIME position:ccp(g_ScrrenSize.width + itemBack_.contentSizeInPixels.width, 0.0f)];
	_CallBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackItemBackActionOver:)];
	_Seq = [CCSequence actions:_Action, _CallBack, nil];
	[itemBack_ runAction:_Seq];
}

- (void)CallBackItemBackActionOver:(id)Sender
{
	[g_SceneLibrary SceneChangeToMenu];
}

@end